package com.example.database.database;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.DatabaseConfiguration;
import androidx.room.InvalidationTracker;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.sqlite.db.SupportSQLiteDatabase;
import androidx.sqlite.db.SupportSQLiteOpenHelper;

import com.example.database.dao.UserDao;
import com.example.database.entity.UserEntity;

@Database(entities = {UserEntity.class},version = 1,exportSchema = false)
public abstract class UserAppDataBase extends RoomDatabase{

    public abstract UserDao userDao();

    public static final String DATABASE_NAME = "UserEntity";

    private static UserAppDataBase sInstance;

    public static UserAppDataBase getInstance(Context context) {
        if (sInstance == null) {
            synchronized (UserAppDataBase.class) {
                if (sInstance == null) {
                    sInstance = buildDatabase(context);
                }
            }
        }
        return sInstance;
    }

    private static UserAppDataBase buildDatabase(final Context appContext) {
        return Room.databaseBuilder(appContext,UserAppDataBase.class,DATABASE_NAME)
                .allowMainThreadQueries()
                .addCallback(new Callback() {
                    @Override
                    public void onCreate(@NonNull SupportSQLiteDatabase db) {
                        super.onCreate(db);
                    }

                    @Override
                    public void onOpen(@NonNull SupportSQLiteDatabase db) {
                        super.onOpen(db);
                    }
                })
                .build();
    }
}
